package org.devocean.groupbuying.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.devocean.groupbuying.dao.CustomerDAO;
import org.devocean.groupbuying.domain.Customer;
import org.devocean.groupbuying.utils.UUIDGenerator;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

	@PersistenceContext
	private EntityManager em;

	public long countCustomers() {
		return em.createQuery("SELECT COUNT(c)  FROM Customer c", Long.class)
				.getSingleResult();
	}

	public List<Customer> findAllCustomers() {
		return em.createQuery("select c from Customer c", Customer.class)
				.getResultList();
	}

	public List<Customer> findCustomersEntries(int firstResult, int maxResults) {
		return em.createQuery("SELECT c FROM Customer c", Customer.class)
				.setFirstResult(firstResult).setMaxResults(maxResults)
				.getResultList();
	}

	public void deleteCustomer(int id) {
		final Customer customer = em.find(Customer.class, id);

		if (customer != null) {

			em.remove(customer);
		}

	}

	public Customer saveCustomer(Customer customer) {
		customer.setUuid(UUIDGenerator.generate(customer));
		em.persist(customer);
		return customer;
	}

	public Customer updateCustomer(Customer customer) {
		final Customer merged = em.merge(customer);
		return merged;
	}

	public Customer findCustomerById(int id) {
		return em.find(Customer.class, id);
	}

	public Customer findCustomer(String username) {
		final List<Customer> lst = em
				.createQuery("select c from Customer c where c.userName=:userName",
						Customer.class).setParameter("userName", username)
				.getResultList();
		return lst.get(0);
	}

	public long countCustomerByUserName(String userName) {
		return em
				.createQuery(
						"SELECT COUNT(c)  FROM Customer c where c.userName =?1",
						Long.class).setParameter(1, userName).getSingleResult();
	}

	public Customer findCustomerByUuid(String customerUuid) {
		
		List<Customer> customers = em.createQuery("SELECT cm FROM Customer cm WHERE cm.uuid=:customerUuid",Customer.class)
				.setParameter("customerUuid", customerUuid).getResultList();
		System.out.println(customers);
		if (!customers.isEmpty()) {
			return customers.get(0);
			
		}
		return null;
	}

	public Long countCustomerByEmail(String email) {
		
		 Query query = em
				.createQuery(
						"SELECT COUNT(c)  FROM Customer c WHERE c.email =:email");
		Long count = (Long) query.setParameter("email", email).getSingleResult();
		System.out.println("count : "+count);
		return count;
	}

	public Customer findCustomerByEmail(String email) {
		List<Customer> customers = em.createQuery("SELECT cm FROM Customer cm WHERE cm.email=:email",Customer.class)
				.setParameter("email", email).getResultList();
		System.out.println(customers);
		if (!customers.isEmpty()) {
			return customers.get(0);
			
		}
		return null;
	}

}
